package cn.hsa.xxx.bookData.bo.impl;

import cn.hsa.xxx.bookData.bo.BookCommodityBO;
import cn.hsa.xxx.bookData.dao.BookCommodityDAO;
import cn.hsa.xxx.api.dto.BookCommodityDTO;
import cn.hsa.xxx.bookData.dto.BookCommodityDTOConverter;
import cn.hsa.xxx.bookData.entity.BookCommodityDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
@Slf4j
public class BookCommodityBOImpl implements BookCommodityBO {

    @Resource
    BookCommodityDAO bookCommodityDAO;

    @Autowired
    BookCommodityDTOConverter bookCommodityDTOConverter;

    @Override
    public BookCommodityDTO add(BookCommodityDTO bookCommodityDTO) {
        BookCommodityDO bookCommodityDO = bookCommodityDTOConverter.convertToBookCommodityDO(bookCommodityDTO);
        bookCommodityDAO.insert(bookCommodityDO);
        return bookCommodityDTOConverter.convertToBookCommodityDTO(bookCommodityDO);
    }

    @Override
    public String delete(String id) {
        bookCommodityDAO.deleteById(id);
        return "删除成功";
    }

    @Override
    public BookCommodityDTO get(String id) {
        BookCommodityDO bookCommodityDO = bookCommodityDAO.selectById(id);
        BookCommodityDTO bookCommodityDTO = bookCommodityDTOConverter.convertToBookCommodityDTO(bookCommodityDO);
        return bookCommodityDTO;
    }

    @Override
    public String update(BookCommodityDTO bookCommodityDTO) {
        BookCommodityDO bookCommodityDO = bookCommodityDTOConverter.convertToBookCommodityDO(bookCommodityDTO);
        bookCommodityDAO.updateById(bookCommodityDO);
        return "更新成功";
    }

    @Override
    public List<BookCommodityDTO> getPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<BookCommodityDO> bookCommodityDOList = bookCommodityDAO.selectList(null);

        PageInfo pageInfo = new PageInfo(bookCommodityDOList);
        long totalRecords = pageInfo.getTotal();
        long totalPages = pageInfo.getPages();

        List<BookCommodityDTO> bookCommodityDTOList = new ArrayList<>();
        for (BookCommodityDO bookCommodityDO : bookCommodityDOList) {
            bookCommodityDTOList.add(bookCommodityDTOConverter.convertToBookCommodityDTO(bookCommodityDO));

        }

        return bookCommodityDTOList;
    }

    @Override
    public BookCommodityDTO getByBookUuid(String id) {
        BookCommodityDO bookCommodityDO = bookCommodityDAO.selectByBookUuid(id);
        BookCommodityDTO bookCommodityDTO = bookCommodityDTOConverter.convertToBookCommodityDTO(bookCommodityDO);
        return bookCommodityDTO;
    }
}
